package com.example.sax;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Person;
import android.os.Bundle;
import android.util.Log;

import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlPullParserException;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        initSAX();
        try {
            List<Bean> result = XMLUtil.parsBeanList(getAssets().open("person_list.xml"));
            for (Bean person : result)
            {
                Log.d(TAG,
                        "id:" + person.getId() +
                                "  姓名:" + person.getName() +
                                "  年龄:" + person.getAge());
            }
        } catch (XmlPullParserException | IOException e) {
            e.printStackTrace();
        }
    }
    private void initSAX()
    {
        try {
            //获取xml数据流
            InputStream inputStream = getAssets().open("person_list.xml");
            //创建SAX解析工具类
            Myhandler myhandler = new Myhandler();
            //获取SAX解析器
            SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
            //开始解析
            saxParser.parse(inputStream,myhandler);

            inputStream.close();

            List<Bean> result = myhandler.getmBeanList();

            for (Bean person : result)
            {
                Log.d(TAG,
                        "id:" + person.getId() +
                            "  姓名:" + person.getName() +
                            "  年龄:" + person.getAge());
            }
        } catch (SAXException | ParserConfigurationException | IOException e)
        {
            e.printStackTrace();
        }
    }
}